// pages/usercenter/identity.js
const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],

  /**
   * 页面的初始数据
   */
  data: {
    reqData: {
      // 性别：0=男, 1=女, 2=未知
      sex: 0,
      avatar: "",
      birth: '',
      email: '',
      nickName: ""
    },
    sexArr: ['男', '女'],
    thumb: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('user', );
    let user = getApp().globalData.userInfo
    this.setData({
      "reqData.sex": user.sex,
      "reqData.avatar": user.avatar,
      "reqData.nickName": user.nickName,
      "reqData.email": user.email,
      "reqData.birth": user.birth,
      thumb: this.get_imgUrl(user.avatar)
    })
  },
  input_change(e) {
    console.log('input_change', e);
    this.data.reqData[e.currentTarget.dataset.field] = e.detail.value
  },
  onChooseAvatar(e) {
    console.log("onChooseAvatar", e);
    // const { avatarUrl } = e.detail 
    // this.setData({
    //   avatarUrl,
    // })
    this.upload_avatar(e.detail.avatarUrl)
  },
  avatar_click() {
    wx.chooseMedia({
      camera: 'front',
      count: 1,

      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (result) => {
        console.log('头像', result);
        this.upload_avatar(result.tempFiles[0].tempFilePath)
      },
      fail(err) {
        console.log(err);
        // wx.showToast({
        //   title: JSON.stringify(err),
        // })
      }

    })
  },
  upload_avatar(filePath) {
    wx.uploadFile({
      filePath: filePath,
      name: 'file',
      url: this.data.host + "/v1/resources/public",

      success: (result) => {
        console.log('上传', result);
        let res = JSON.parse(result.data);
        console.log(res);
        if (res.code == 200) {
          this.setData({
            "reqData.avatar": res.data.url,
            thumb: this.get_imgUrl(res.data.url)
          })
        } else {
          wx.showToast({
            title: res.msg || '上传失败',
            icon: "none",
          })
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  submit_click() {
    this.request({
      url: this.data.host + '/v1/users/me',
      method: "put",
      data: this.data.reqData,
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded'
      // }
    }, res => {
      wx.showToast({
        title: '更新成功',
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      }, 1000);
    })
  },
  sex_click() {
    wx.showActionSheet({
      itemList: this.data.sexArr,
      success: (result) => {
        console.log("result", result);
        this.setData({
          "reqData.sex": result.tapIndex
        })
      },

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})